
Character = function ()
{
	/********************************************
	/*				Properties					
	/********************************************/
	
		this.m_Lerp;
		this.m_Sprite;	/*!< Sprite attached to the current character			*/
		this.m_Entity;	/*!< Entity on wich the current character is attached	*/
	
	/********************************************
	/*				Constructor					
	/********************************************/
	
		Behaviour.call(this);
	
		Mouse.MouseDownHandlers.push(new Method(this, this.MoveCharacter));
}

Character.prototype.Init	=	function()
{
	this.m_Sprite 	= new Sprite(g_Image, 9, 8, 1, 80, this.m_Entity);
	this.m_Lerp		= new Lerp();
}

Character.prototype.Attached	=	function(entity)
{
	Behaviour.prototype.Attached.call(this, entity);
}

Character.Instantiate	=	function(transform)
{
	var character = new Character();
	
	character = Behaviour.Instantiate(transform, character);

	character.m_Entity.m_Mesh	=	Mesh.NewRect(80,80);
	
	character.m_Entity.m_mesh 	= character.m_Entity;
	character.Init();
	
	return character;
}

Character.prototype.Update	=	function()
{
	Behaviour.prototype.Update.call(this);
	
	if( this.m_Entity!=undefined)
	{
		this.m_Sprite.Update();

		
		
		if(this.m_Lerp.m_End==false)
		{
			this.m_Entity.m_Transform.SetPositionv(this.m_Lerp.Update());
		}
	}
}

Character.prototype.MoveCharacter = function(x, y)
{
	if(Mouse.Buttons[2]==MouseState.Down)
	{
		var worldMousePosition = View.CurrentView.MouseToWorld(x,y,0);
		var ray = View.CurrentView.RayFromScreen(x,y);
		Raycast.CheckIntersection(ray);
		

		var rightVec = new Vec4(1.0, 0.0, 0.0, 1.0);
		
		var positionClick = new Vec4(worldMousePosition.x, worldMousePosition.y, 0.0, 1.0);

		console.log("Start "+ this.m_Transform.m_Position.x);
		console.log("End "	+ positionClick.x);
		
		this.m_Lerp.StartVelocity(0.16, this.m_Transform.m_Position, positionClick );
		
		var vecPosition = 	this.m_Entity.m_Transform.m_Position;
		var vec 		= 	positionClick.Minus(vecPosition);

		var div = (vec.Length()+rightVec.Length());
		var angle=Math.acos( (Vec4.Dot(rightVec, vec))/div );
		
			if(vec.y>0)
			{
				angle = -angle;
			}
			
			if((angle> -Math.PI/8) && (angle< Math.PI/8))
			{
			//	console.log(6);
				this.m_Sprite.m_CurrentAnimation=1;
			}
			else if((angle> Math.PI/8) && (angle< 3*Math.PI/8))
			{
				this.m_Sprite.m_CurrentAnimation=2;
			//	console.log(9);
			}
			else if((angle> 3*Math.PI/8) && (angle< 5*Math.PI/8))
			{
				this.m_Sprite.m_CurrentAnimation=3;
			//	console.log(8);
			}
			else if((angle> 5*Math.PI/8) && (angle< 7*Math.PI/8))
			{
				this.m_Sprite.m_CurrentAnimation=4;
				//console.log(7);
			}
			else if((angle> 7*Math.PI/8) || (angle< -7*Math.PI/8))
			{
				this.m_Sprite.m_CurrentAnimation=5;
			//	console.log(4);
			}
			else if((angle> -7 *Math.PI/8) && (angle< -5*Math.PI/8))
			{
				//console.log(1);
				this.m_Sprite.m_CurrentAnimation=6;
			}
			else if((angle> -5 *Math.PI/8) && (angle< -3*Math.PI/8))
			{	
				//console.log(2);
				this.m_Sprite.m_CurrentAnimation=7;
			}
			else if((angle> -3 *Math.PI/8) && (angle< -1*Math.PI/8))
			{	
				//console.log(3);
				this.m_Sprite.m_CurrentAnimation=0;
			}
		}
}
